package Servlets;

import java.io.IOException;

import javaBeans.LoginObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Services.LoginService;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		LoginService loginService = new LoginService();
		HttpSession session = request.getSession();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		// tests
		System.out.println("Servlet requestParameter username: " + username);
		System.out.println("Servlet requestParameter password: " + password);
		// endtests

		LoginObject loginDetails = loginService.confirmLogin(username, password);
		
		// tests
		System.out.println("Testing memberID: " + loginDetails.getIdmember());
		System.out.println("Testing memberUsername: " + loginDetails.getUsername());
		// endtests
		if (loginDetails.getIdmember() != 0){
			session.setAttribute("username", loginDetails.getUsername());
			session.setAttribute("memberID", loginDetails.getIdmember());
			session.setAttribute("loginobject", loginDetails);
			response.sendRedirect("indexLoggedIn.jsp");
		}else{
			session.setAttribute("username", loginDetails.getUsername());
			response.sendRedirect("indexFailed.jsp");
		}
	}
}
